package com.xiao.watch_tool;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @CreateBy 花和尚  1130365579@qq.com  2023/3/10 15:57
 * @Version 1.0
 */
public class JConsoleTestCaseMemory {
    static class OOMObject {
        // 占位 64 字节
        private byte[] placeholder = new byte[64 * 1024];
    }

    public static void fillHeap(int num) throws InterruptedException {
        ArrayList<OOMObject> oomObjects = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            // 睡眠 50ms
            Thread.sleep(50);
            oomObjects.add(new OOMObject());
        }
        // 在这里调用 gc 不能对 oomObjects 列表进行清理，因为 当前方法没有退出，oomObjects 对象仍被引用
        System.gc();
    }

    /**
     * VM 参数
     *
     * @param args
     */
    public static void main(String[] args) throws InterruptedException, IOException {
        fillHeap(1000);
        System.out.println("堆内存填充执行完毕！");
        System.gc();
        final int read = System.in.read();
    }
}
